(function($, doc) {
	$.init();
	$.plusReady(function() {
		var old_back = $.back;
		var settings = app.getSettings();
		var name_temp = plus.storage.getItem(KeyCfg.userName);
		if(name_temp.length>2){
			name_temp = name_temp.substring(0,2)+'****'+name_temp.substring(name_temp.length-1,name_temp.length);
		}else{
			name_temp = name_temp.substring(0,1)+'****';
		}
		document.getElementById('userName').innerHTML = name_temp;
		var forgetButton = doc.getElementById("btn_forget");
		var otherButton = doc.getElementById('btn_other');
		var locker = doc.querySelector('.mui-locker');
		var clearStateAndToLogin = function() {
			settings.gestures = '';
			settings.autoLogin = false;
			plus.storage.removeItem(KeyCfg.JSESSIONID);
			app.setSettings(settings);
			var win = plus.webview.getWebviewById(appid);
			win.evalJS("openTab(null,subpages2[0]);");
			win.show();
		};
		otherButton.addEventListener('tap',function(event){
			plus.storage.removeItem(KeyCfg.JSESSIONID);
			var win = plus.webview.getWebviewById(appid);
			win.evalJS("openTab(null,subpages2[0]);");
			win.show();
		});
		forgetButton.addEventListener('tap', function(event) {
			clearStateAndToLogin();
		});
		var tryNumber = 5;
		locker.addEventListener('done', function(event) {
			var rs = event.detail;
			if (settings.gestures == rs.points.join('')) {
				var sessionId = plus.storage.getItem(KeyCfg.JSESSIONID);
				var userId = plus.storage.getItem(KeyCfg.userId);
				var userName = plus.storage.getItem(KeyCfg.userName);
				var pwd = plus.storage.getItem(KeyCfg.userPwd);
				var url = UrlConfig.muser_login;
				var formDatas = {userName:userName,pwd:pwd,callback:'jquery'};
				if(!userName || !pwd || userName==null || pwd==null){
					plus.storage.removeItem(KeyCfg.JSESSIONID);
					var win = plus.webview.getWebviewById(appid);
					win.evalJS("openTab(null,subpages2[0]);");
					win.show();
				}else{
					$.ajax(url, {
				        data : formDatas,
				        dataType : 'jsonp',  
				        crossDomain: true,  
				        success: function(data) {
				        	data = data.replace("jquery('",'').replace("')",'');
				        	data = JSON.parse(data);
				        	if(data.success){
				        		sessionId = data[KeyCfg.JSESSIONID];
								userId = data[KeyCfg.userId];
								userName = data[KeyCfg.userName];
								plus.storage.setItem(KeyCfg.JSESSIONID, sessionId);
								plus.storage.setItem(KeyCfg.userId, userId);
								plus.storage.setItem(KeyCfg.userName, userName);
								var settings = app.getSettings();
								var date = new Date();
								settings.overTime = date.getTime();
								app.setSettings(settings);
								old_back();
				        	}else{
				        		var win = plus.webview.getWebviewById(appid);
								win.evalJS("openTab(null,subpages2[0]);");
								win.show();
				        	}
				        },
				        error :function(data){
				        }
				    });
				}
				rs.sender.clear();
			} else {
				tryNumber--;
				rs.sender.clear();
				if (tryNumber > 0) {
					plus.nativeUI.toast('解锁手势错误，还能尝试 ' + tryNumber + ' 次。');
				} else {
					clearStateAndToLogin();
				}
			}
		}, false);
		var backButtonPress = 0;
		$.back = function(event) {
			backButtonPress++;
			if (backButtonPress > 1) {
				plus.runtime.quit();
			} else {
				plus.nativeUI.toast('再按一次退出应用');
			}
			setTimeout(function() {
				backButtonPress = 0;
			}, 1000);
			return false;
		};
	});
}(mui, document));